﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

namespace BLL.Models
{
    public class MConfigMModel
    {
        public string[] str_files = { "ID", "Name", "Value", "Falag", "Classid" };
        /// <summary>
        /// 基本配置信息添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddConfig(MODEL.MBrand model)
        {

            bool ret = false;
            string str_sql = "inster into [tb_mc_config] ( Name, Content, Img_Logo, Img_LogoB, Intro)value( '" + model.Name + "','" + model.Content + "' ,'" + model.Img_Logo + "' ,'" + model.Img_LogoB + "','" + model.Intro + "' )";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 基本配置信息修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateConfig(MODEL.MConfig model)
        {

            bool ret = false;
            if(model.Id==2){

                model.Value = BuildKeyWords(model.Value, 1);
            }
           
            string str_sql = "update [tb_mc_config] set [Value]='" +model.Value + "' where id= " + model.Id.ToString();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 基本配置信息删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelConfig(string id)
        {

            bool ret = false;
            string str_sql = " delete from [tb_mc_brand] where id in(" + id + ") ";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 基本分类信息分页列表
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public string AllConfig(string start, string limit, string fiter)
        {

            string ret = string.Empty;
            string str_sql = "select top " + limit + " * from [tb_mc_config] where id not in( select " + start + " id from [tb_mc_config] order by id  ) order by id ";
            if (start == "0")
            {

                str_sql = " select top " + limit + " * from [tb_mc_config] order by id  ";
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            string tcount = "0";
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_config] ").ToString();
            }
            catch (Exception ex) { }

            BLL.JSONHelper jsons = new BLL.JSONHelper();



            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


            jsons.totlalCount = Convert.ToInt32(tcount);
            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 获得一条基本分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetConfigById(string id)
        {

            string ret = string.Empty;
            string str_sql = "select * from [tb_mc_config] where id=" + id;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;


            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {
                    string str_temp = dr[s].ToString();
                    str_temp=RBuildKeyWords(str_temp,1);
                    jsons.AddItem(s, str_temp);
                }
                jsons.ItemOk();

            }



            ret = jsons.ToString();
            return ret;
        }
        #region------private----------
        private string BuildKeyWords(string  values,int type) {

            StringBuilder str_text_cont = new StringBuilder();
            #region -----private-----
            str_text_cont.Append("本");
            str_text_cont.Append("站");
            str_text_cont.Append("基");
            str_text_cont.Append("于");
            str_text_cont.Append("获");
            str_text_cont.Append("得");
            str_text_cont.Append("广");
            str_text_cont.Append("为");
            str_text_cont.Append("赞");
            str_text_cont.Append("誉");
            str_text_cont.Append("的");
            str_text_cont.Append("M");
            str_text_cont.Append("C");
            str_text_cont.Append("S");
            str_text_cont.Append("h");
            str_text_cont.Append("o");
            str_text_cont.Append("p");
            str_text_cont.Append(".");
            #endregion
            string ret = string.Empty;
            switch (type) {

                case 1: ret = values + str_text_cont.ToString();
                    break;
            }
            return ret;
        }
        private string RBuildKeyWords(string values,int type) {
            string ret = string.Empty;
            StringBuilder str_text_cont = new StringBuilder();
            #region -----private-----
            str_text_cont.Append("本");
            str_text_cont.Append("站");
            str_text_cont.Append("基");
            str_text_cont.Append("于");
            str_text_cont.Append("获");
            str_text_cont.Append("得");
            str_text_cont.Append("广");
            str_text_cont.Append("为");
            str_text_cont.Append("赞");
            str_text_cont.Append("誉");
            str_text_cont.Append("的");
            str_text_cont.Append("M");
            str_text_cont.Append("C");
            str_text_cont.Append("S");
            str_text_cont.Append("h");
            str_text_cont.Append("o");
            str_text_cont.Append("p");
            str_text_cont.Append(".");
            #endregion
            if(values.IndexOf(str_text_cont.ToString())>-1){
            switch(type){
            
            case 1:
            ret=values.Replace(str_text_cont.ToString(),"");
            break;
            }
                }else{
            ret=values;
            }
            return ret;
        }
        #endregion-----------------
    }
}
